package com.zzyl.nursing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.DeviceShadowDataVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * deviceController
 * 
 * @author xiecong
 * @date 2025-05-30
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "device相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询device列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取device列表")
    public TableDataInfo<Device> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 查询所有产品列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:allProduct')")
    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public R<List<ProductVo>> allProduct() {
        List<ProductVo> list = deviceService.allProduct();
        return R.ok(list);
    }

    /**
     * 从物联网平台同步产品列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:syncProductList')")
    @PostMapping("/syncProductList")
    @ApiOperation(value = "从物联网平台同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return AjaxResult.success();
    }

    /**
     * 注册设备
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:register')")
    @PostMapping("/register")
    @Log(title = "device", businessType = BusinessType.INSERT)
    @ApiOperation(value = "注册设备")
    public R register(@RequestBody DeviceDto dto){
        deviceService.register(dto);
        return R.ok();
    }

    /**
     * 查询设备详细数据
     */
    @GetMapping("/{iotId}")
    @ApiOperation(value = "查询设备详细数据")
    public R<DeviceDetailVo> getInfo(@PathVariable String iotId){
        DeviceDetailVo vo =deviceService.getInfo(iotId);
        return R.ok(vo);
    }

    /**
     * 查看设备上报的数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation(value = "查看设备上报的数据")
    public R<List<DeviceShadowDataVo>> queryServiceProperties(@PathVariable String iotId){
        List<DeviceShadowDataVo> vo =deviceService.queryServiceProperties(iotId);
        return R.ok(vo);
    }
}
